<?php $this->_extends('_layouts/report_layout'); ?>
<?php $this->_block('title'); ?>
	Pixels Manager
<?php $this->_endblock(); ?>

<?php $this->_block('contents'); ?>
<div id="content" class="box">
	<h1>Pixel Manager</h1>
	<div id="menu" class="box">
		<ul class="box">
			<li><a href="<?php echo url('pixelManager/insert');?>"><span>Add</span></a></li>
			<li><a href="<?php echo url('pixelManager/tblindex');?>"><span>Pixel_Managerment Table</span></a></li>
		</ul>
	</div>
		<?php 
			if(isset($infos)):
				foreach($infos as $info): ?>
					<p class="msg info"><?php echo $info;?></p>
		<?php	endforeach;
			endif; ?>
            
		<?php 
			if(isset($errors)):
				foreach($errors as $error): ?>
					<p class="msg error"><?php echo $error;?></p>
		<?php	endforeach;
			endif; ?>
            
	<table class="full-size">
		<tbody>
			<tr>
				<th>Date</th>
				<th>Campaign</th>
				<th>Publisher</th>
				<th>Placement</th>
				
				<th>Creative</th>
				<th>Impressions</th>
				<th>Our Clicks</th>
				<th>Our Conversions</th>
				<th>Our Conversion Rate</th>
				<th>Publisher Clicks</th>
				<th>Publisher Conversions</th>
				<th>Publisher CR</th>
				<th>Scrub Rate</th>
				<th>Revenue</th>
				<th>Cost</th>
				<th>Gross Profit</th>
				<th>Gross Margin</th>
				<th>ROAS</th>
				<th>Suppression Rate</th>
				<th>Publisher CPA</th>
				<th>Publisher EPC</th>
				<th>Publisher RPA</th>
				<th>Our CPA</th>
				<th>Our RPA</th>
			</tr>
			<?php
				if(!empty($pixels)):
					foreach ($pixels as $k=>$pixel):
                    	$grossProfit = $pixel['revenue'] - $pixel['cost'];
                    	$ourConversion = $pixel['conversion'] + $pixel['suppress'];
					?>
                        <tr <?php if ($bg) echo 'class="bg"';?>>
                            <td><?php echo $date;?></td>
                            <td><?php echo ($pixel['cam'] == '')? '-' : $pixel['cam'];?></td>
                            <td><?php echo ($pixel['pub'] == '')? '-' : $pixel['pub'];?></td>
                            <td><?php echo ($pixel['pla'] == '')? '-' : $pixel['pla'];?></td>
                            
                            <td><?php echo ($pixel['cre'] == '')? '-' : $pixel['cre'];?></td>
							<td><?php echo ($pixel['impression'] == '')? '0' : $pixel['impression'];?></td>
							<td><?php echo ($pixel['click'] == '')? '0' : $pixel['click'];?></td>
							<td><?php echo ($ourConversion == '')? '0' : $ourConversion;?></td>
							<td><?php echo Util::divide($ourConversion, $pixel['click'], 2, true);?></td>
							<td><?php echo ($pixel['click'] == '')? '0' : $pixel['click'];?></td>
							<td><?php echo ($pixel['conversion'] == '')? '0' : $pixel['conversion'];?></td>
							<td><?php echo Util::divide($pixel['conversion'], $pixel['click'], 2, true);?></td>
							<td><?php echo number_format((1 - Util::divide($pixel['conversion'], $ourConversion, 4)) * 100, 2); ?>%</td>
							<td><?php echo ($pixel['revenue'] == '')? '0' : $pixel['revenue'];?></td>
							<td><?php echo ($pixel['cost'] == '')? '0' : $pixel['cost'];?></td>
							<td><?php echo number_format($grossProfit, 2); ?></td>
							<td><?php echo Util::divide($grossProfit, $pixel['revenue'], 2, true);?></td>
							<td><?php echo Util::divide($grossProfit, $pixel['cost'], 2, true);?></td>
							<td><label id="<?php echo $k.'_'.$pixel['id']; ?>" class="modifysrbtn" style="text-decoration:underline; color:#06F; cursor:pointer;"><?php echo ($pixel['peak'] == '')? '0' : $pixel['peak'];?>%</label></td>
							<td><?php echo Util::divide($pixel['cost'], $pixel['conversion'], 2, true);?></td>
							<td><?php echo Util::divide($pixel['cost'], $pixel['click'], 2, true);?></td>
							<td><?php echo Util::divide($pixel['revenue'], $pixel['conversion'], 2, true);?></td>
							<td><?php echo Util::divide($pixel['cost'], $ourConversion, 2, true);?></td>
							<td><?php echo Util::divide($pixel['revenue'], $ourConversion, 2, true);?></td>
                        </tr>
			<?php	endforeach;
				endif; ?>
		</tbody>
	</table>
</div>
<div>
	<table id="modifysrbox">
      		<tr>
           		<td>Suppress Rate:</td>
           		<td>
               		<input type="text" id='modifysrval' />
               		<input type="button" value="Set" id="setSuppressRate"  />
               		<label id="setSuppressRatelabel" style="color:red;"></label>
            	</td>
         	</tr>
 	</table>
</div>
<input type="hidden" id="modifysrbtn_id" />

<script type="text/javascript">
	$('#modifysrbox').dialog({
		autoOpen: false,
		height: '200',
		width: '300',
		modal: true,
		buttons: {OK: function(){
						$( this ).dialog( "close" );	
					  }
				 },
	});

	$('.modifysrbtn').click(function(){
		$('#modifysrbtn_id').val($(this).attr('id'));
		$('#modifysrbox').dialog('open');
		
		$('#modifysrval').val('');
	});

	$("#setSuppressRate").live('click', function(){
		var btn = $(this);
		$.ajax({
			type: 'POST',
			url: "<?php echo url('pixelmanager/setpeak'); ?>",
			data: {
					id: $('#modifysrbtn_id').val().split('_')[1],
					peak: $('#modifysrval').val()
				  },
			success: function(data){
						if(data=='OK'){
							$('#' + $('#modifysrbtn_id').val() ).html(($('#modifysrval').val()*1).toFixed(2) + '%');
							$("#setSuppressRatelabel").html('OK!');	
							$('#modifysrbox').dialog('close');
						}else{
							$("#setSuppressRatelabel").html('Invalidate password!');
						}
					 }
		});
	});
	
</script>

<?php $this->_endblock(); ?>